﻿
using System.Collections.Generic;
using System.Linq;
using Channelvn.Cached;
using Core.Library;

namespace DataObject.Vote
{
    public class VoteCache
    {
        public static bool Vote_CheckIsVoteMemberId(int fromId, int toId)
        {
            var listParams = new object[] { fromId, toId };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("Vote_CheckIsVoteMemberId", listParams);
            var fromcache = CacheController.GetCache(Constants.VOTE_CACHE_CATEGORY, cacheName);
            if (fromcache != null)
            {
                return (bool)fromcache;
            }
            else
            {
                var result = false;
                var lstVoted = Vote_GetVoteLogByFromID(fromId);
                if (lstVoted != null && lstVoted.Where(d => d.VoteLog_ToID == toId).Count() > 0)
                {
                    result = true;
                }
                CacheController.Add(Constants.VOTE_CACHE_CATEGORY, cacheName, result, Constants._5min);
                return result;
            }

        }

        public static List<VoteLogObject> Vote_GetVoteLogByFromID(int id)
        {
            var listParams = new object[] { id };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("Vote_GetVoteLogByFromID", listParams);
            var fromcache = CacheController.GetCache(Constants.VOTE_CACHE_CATEGORY, cacheName);
            if (fromcache != null)
            {
                return (List<VoteLogObject>)fromcache;
            }
            else
            {
                var o = VoteController.Vote_GetVoteLogByFromID(id);
                CacheController.Add(Constants.VOTE_CACHE_CATEGORY, cacheName, o, Constants._5min);
                return o;
            }
        }

        public static int Members_GetVoteByRound(int id, int round)
        {
            var listParams = new object[] { id, round };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("Members_GetVoteByRound", listParams);
            var fromcache = CacheController.GetCache(Constants.VOTE_CACHE_CATEGORY, cacheName);
            if (fromcache != null)
            {
                return (int)fromcache;
            }
            else
            {
                var o = VoteController.Members_GetVoteByRound(id, round);
                CacheController.Add(Constants.VOTE_CACHE_CATEGORY, cacheName, o, Constants._5min);
                return o;
            }
        }

        public static bool Vote_AntiCloneLowLevel(int toId, string ip)
        {
            var listParams = new object[] { toId, ip };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("Vote_AntiCloneLowLevel", listParams);
            var fromcache = CacheController.GetCache(Constants.DATA_CACHE_CATEGORY, cacheName);
            if (fromcache != null)
            {
                var count = ConvertUtility.ConvertTo<int>(fromcache);
                count += 1;
                if (count <= Const.CloneLowLevel)
                {
                    CacheController.Add(Constants.DATA_CACHE_CATEGORY, cacheName, count, Constants._30min);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                CacheController.Add(Constants.DATA_CACHE_CATEGORY, cacheName, 0, Constants._30min);
                return true;
            }
        }

        public static bool Vote_AntiCloneNormalLevel(int toId, string ip)
        {
            var listParams = new object[] { toId, ip };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("Vote_AntiCloneNormalLevel", listParams);
            var fromcache = CacheController.GetCache(Constants.DATA_CACHE_CATEGORY, cacheName);
            if (fromcache != null)
            {
                var count = ConvertUtility.ConvertTo<int>(fromcache);
                count += 1;
                if (count <= Const.CloneNormalLevel)
                {
                    CacheController.Add(Constants.DATA_CACHE_CATEGORY, cacheName, count, Constants._30min);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                CacheController.Add(Constants.DATA_CACHE_CATEGORY, cacheName, 0, Constants._30min);
                return true;
            }
        }

        public static bool Vote_AntiCloneHighLevel(int toId, string ip)
        {
            var listParams = new object[] { toId, ip };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("Vote_AntiCloneHighLevel", listParams);
            var fromcache = CacheController.GetCache(Constants.DATA_CACHE_CATEGORY, cacheName);
            if (fromcache != null)
            {
                return false;
            }
            else
            {
                CacheController.Add(Constants.DATA_CACHE_CATEGORY, cacheName, 0, Constants._30min);
                return true;
            }
        }

        public static bool Vote_IPFloodVote(string ip)
        {
            var listParams = new object[] { ip };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("Vote_IPFloodVote", listParams);
            var fromcache = CacheController.GetCache(Constants.DATA_CACHE_CATEGORY, cacheName);
            if (fromcache != null)
            {
                var count = ConvertUtility.ConvertTo<int>(fromcache);
                count += 1;
                if (count <= Const.IPFloodVote)
                {
                    CacheController.Add(Constants.DATA_CACHE_CATEGORY, cacheName, count, Constants._30min);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                CacheController.Add(Constants.DATA_CACHE_CATEGORY, cacheName, 0, Constants._30min);
                return true;
            }
        }

        public static bool Vote_DelayIPFloodVote(string ip)
        {
            var listParams = new object[] { ip };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("Vote_DelayIPFloodVote", listParams);
            var fromcache = CacheController.GetCache(Constants.DATA_CACHE_CATEGORY, cacheName);
            if (fromcache != null)
            {
                return false;
            }
            else
            {
                CacheController.Add(Constants.DATA_CACHE_CATEGORY, cacheName, 0, Constants._1min);
                return true;
            }
        }
    }
}
